# -*- encoding:utf-8 -*-
# !/usr/bin/env python
# @Date: 2019-04-26 11:01:56
# @Author: fuxueshuang

# 风险事项预警
import json
from flask import Blueprint, g

from config import config
from lib.funcs import sync_to_anguan
from lib.validform import V, ValidateForm

app = Blueprint(__name__ + "_app", __name__)


# 风险事项预警
@app.route('/api/warning/issuedWarning', methods=['POST'])
# @auth.permission("warning")
def warning_issuedWarning():
    try:
        form = ValidateForm(
            id=['风险事项id', V.required()],
            warnunit=['预警机构集', V.required()]
        )
        (flag, data) = form.validate()
        if not flag:
            data['status'] = 'fail'
            return json.dumps(data)

        g.cursor.execute("""select a.risk_event_id as risk_event_id, '预警' as send_type,
                                a.risk_event_name as risk_event_name,
                                case when a.risk_event_level = '1' then '一般事件'
                                    when a.risk_event_level = '2' then '较大事件'
                                    when a.risk_event_level = '3' then '重大事件'
                                    when a.risk_event_level = '4' then '特大事件' end as risk_event_level,
                                    a.risk_event_type, a.risk_event_content as risk_event_description,
                                    a.solution as risk_event_solution, a.remark as remark,
                                    coalesce(json_agg(distinct b) filter (where b.node_id is not null), '[]') as impact_unit,
                                    coalesce(json_agg(distinct c) filter (where c.info_sys_id is not null), '[]') as impact_sys,
                                    coalesce(json_agg(distinct d) filter (where d.sys_node_id is not null), '[]') as impact_node
                            from h_risk_event_info a
                            left join (
                                select f.agency_id as node_id, f.agency_name as node_name
                                from h_risk_event_info a
                                left join h_event_element_info b
                                on a.risk_event_id=b.risk_event_id
                                left join h_event_element_detail c
                                on c.element_id=b.element_id
                                left join h_system_node d
                                on c.sys_id=d.sys_node_id
                                left join h_information_system e
                                on d.info_sys_id=e.info_sys_id
                                left join sys_agency_info f
                                on e.agency_id=f.agency_id
                                where a.risk_event_id=%(id)s and f.agency_id is not null
                                group by f.agency_id, f.agency_name
                            ) b
                            on 1=1
                            left join (
                                select d.info_sys_id, e.info_sys_name
                                from h_risk_event_info a
                                left join h_event_element_info b
                                on a.risk_event_id=b.risk_event_id
                                left join h_event_element_detail c
                                on c.element_id=b.element_id
                                left join h_system_node d
                                on c.sys_id=d.sys_node_id
                                left join h_information_system e
                                on d.info_sys_id=e.info_sys_id
                                where a.risk_event_id=%(id)s and d.info_sys_id is not null
                                group by d.info_sys_id, e.info_sys_name
                            ) c
                            on 1=1
                            left join (
                                select d.sys_node_id, d.sys_node_name
                                from h_risk_event_info a
                                left join h_event_element_info b
                                on a.risk_event_id=b.risk_event_id
                                left join h_event_element_detail c
                                on c.element_id=b.element_id
                                left join h_system_node d
                                on c.sys_id=d.sys_node_id
                                where a.risk_event_id=%(id)s and d.sys_node_id is not null
                                group by d.sys_node_id, d.sys_node_name
                            ) d
                            on 1=1
                            where a.risk_event_id=%(id)s
                            group by a.risk_event_id, a.risk_event_name, a.risk_event_level, a.risk_event_type, a.risk_event_content, a.solution, a.remark
                        """, data)
        result = g.cursor.fetchone()
        g.cursor.execute("""select agency_id as node_id, agency_name as node_name
                            from sys_agency_info
                            where agency_id in (select Unnest(string_to_array(%(warnunit)s,',')))
                        """, data)
        result['advince_node_info'] = g.cursor.fetchall()
        sync_to_anguan(module='风险预警', opreaing='预警下发下发', url='{}/upoaws/api/warning/issuedWarning'.format(config.SAFETY_MANAGEMENT_IP), method='POST', data=result)
        return json.dumps({"status": "success", "data": result})
    except Exception, e:
        return json.dumps({"status": "except", 'msg': '查询失败', 'warn': str(e)})
